package cz.muni.fi.pa165.service;

import cz.muni.fi.pa165.dao.MushroomDAO;
import cz.muni.fi.pa165.dto.MushroomDTO;
import cz.muni.fi.pa165.entity.Months;
import cz.muni.fi.pa165.entity.Mushroom;
import cz.muni.fi.pa165.entity.MushroomType;
import cz.muni.fi.pa165.utils.MushroomObjectTransformer;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;

/**
 *
 * @author Marek Tuhársky
 */
@Stateless
@Local(value=MushroomServiceLocal.class)
public class MushroomService implements MushroomServiceLocal {

    @EJB
    private MushroomDAO mushroomDao;
    
    public void setMushroomDao(MushroomDAO mushroomDao) {
        this.mushroomDao = mushroomDao;
    }

    @Override
    public void createMushroom(MushroomDTO mushroomDTO) {
        mushroomDao.createMushroom(MushroomObjectTransformer.mushroomDTOToMushroom(mushroomDTO));
    }

    @Override
    public MushroomDTO getMushroom(Long id) {

        return MushroomObjectTransformer.mushroomToMushroomDTO(mushroomDao.getMushroom(id));
    }

    @Override
    public void updateMushroom(MushroomDTO mushroomDTO) {
        mushroomDao.updateMushroom(MushroomObjectTransformer.mushroomDTOToMushroom(mushroomDTO));
    }

    @Override
    public void removeMushroom(MushroomDTO mushroomDTO) {
        mushroomDao.removeMushroom(MushroomObjectTransformer.mushroomDTOToMushroom(mushroomDTO));
    }

    @Override
    public List<MushroomDTO> findAllMushrooms() {
        List<MushroomDTO> resultList = new ArrayList<MushroomDTO>();
        List<Mushroom> mushroomList = mushroomDao.findAllMushrooms();
        for (Mushroom mushroom : mushroomList) {
            resultList.add(MushroomObjectTransformer.mushroomToMushroomDTO(mushroom));
        }
        return resultList;
    }

    @Override
    public MushroomDTO findMushroomsByName(String name) {
        return MushroomObjectTransformer.mushroomToMushroomDTO(mushroomDao.findMushroomsByName(name));
    }

    @Override
    public List<MushroomDTO> findMushroomsByType(MushroomType type) {
        List<MushroomDTO> resultList = new ArrayList<MushroomDTO>();
        List<Mushroom> mushroomList = mushroomDao.findMushroomsByType(type);
        for (Mushroom mushroom : mushroomList) {
            resultList.add(MushroomObjectTransformer.mushroomToMushroomDTO(mushroom));
        }
        return resultList;
    }

    @Override
    public List<MushroomDTO> findMushroomsByOccurence(Months from, Months to) {
        if(from == null){
            throw new IllegalArgumentException("from is null");
        }
        if(to == null){
            throw new IllegalArgumentException("to is null");
        }
        List<MushroomDTO> resultList = new ArrayList<MushroomDTO>();
        List<Mushroom> mushroomList = mushroomDao.findMushroomsByOccurence(from, to);
        for (Mushroom mushroom : mushroomList) {
            resultList.add(MushroomObjectTransformer.mushroomToMushroomDTO(mushroom));
        }
        return resultList;
    }

}
